package com.aoyama.juc.volatiles;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author ：qs
 * @date ：Created in 2022/6/5
 * @description： 功能描述: CAS引发的ABA问题
 * @modified By：
 */
public class Video19_01 {
    static AtomicReference<Integer> num = new AtomicReference<>(100);

    public static void main(String[] args) {

        new Thread(() ->{
            num.compareAndSet(100, 101);
            num.compareAndSet(101,100);
        },"线程A").start();

        new Thread(() ->{
            //保证A线程已经修改完
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            boolean b = num.compareAndSet(100, 2019);
            System.out.println(b + "\t 当前最新值" + num.get().toString());
        },"线程B").start();
    }
}
